//商品控制
var itemCount = 1;

//点击减号
window.onReduceItemClick = function onReduceItemClick(){
    if(itemCount>0) itemCount--;
    renderItemCount();
}

//点击加号
window.onAddItemClick = function onAddItemClick(){
    itemCount++;
    renderItemCount();
}

//渲染商品数量
function renderItemCount(){
    $("#itemCount").text(itemCount);
}

//点击加入购物车
window.onAddToCartClick = function onAddToCartClick(){
    let uid = sessionStorage.getItem('userId');
    let count = itemCount;
    let itemId = currentItem;
    let formDatas = new FormData();
    formDatas.append('quantity',count);
    //ajax请求后端
    fetch(`http://localhost:8022/api/carts/${uid}/items/${itemId}`,{
        method: 'PATCH',
        mode: 'cors',
        body: formDatas
    })
    .then((response)=>{
        if(!response.ok){
            window.alert('网络错误，请稍后再试!');
        }else{
            return response.json();
        }
    })
    .then((data)=>{
        //判断成功与失败
        if(!(data.status === 1)){
            window.alert('网络错误，请稍后再试!');
        }
        else{
            console.log(data);
            window.alert('添加成功!');
        }
    })
    .catch((error)=>{
        console.log(error);
    })
}

//点击立刻购买
window.onBuyClick = function onBuyClick(){
    //跳转

}

//点击返回
window.onBackClick = function onBackClick(){
    loadCategories(currentNav);
}
